<?php
# @version SVN: $Id: article.php 582 2012-04-05 15:05:38Z novachok $

$ARTICLE = new article($REQUEST->getRequestLast());

if ($commit)
{
   if(empty($ARTICLE->id))
   {
      $requestPath = $REQUEST->getRequestPath();
      $ARTICLE->setCategory($requestPath[($REQUEST->getRequestLength() - 1)]);
      $article_proceed = 'new';
   } else
   {
      $article_proceed = 'edit';
   }

   if($ARTICLE->save($REQUEST->data('f')))
   {
      # Index Article for search
      if(config::read('enabled', 'search'))
      {
         $index_article = array();
         $langs         = $REQUEST->getLangAllId();
         foreach($langs AS $l)
         {
            $index_article[$l]['id']          = $ARTICLE->id;
            $index_article[$l]['cat']         = $ARTICLE->cat;
            $index_article[$l]['lang']        = $l;
            $index_article[$l]['sdate']       = $ARTICLE->sdate('src');
            $index_article[$l]['title']       = $ARTICLE->title($l);
            $index_article[$l]['description'] = $ARTICLE->description($l);
            $index_article[$l]['body']        = $ARTICLE->body($l);
         }

         $SEARCH = new search();
         $SEARCH->addArticle($index_article);

         # TODO: переделать поисковый класс, так чтобы он мог работать при отсутствии морфологического словаря.
         # TODO: Нужно сделать чтобы индексатор работал как observer на событие сохранения, а также на событие восстановления из корзины
      }
      
      $return_article['id'] = $ARTICLE->id;
      $return_article['parent'] = $ARTICLE->cat;
      $return_article['title'] = $ARTICLE->title;

      $params = array();
      $params['exec'] = 'parent.frames[\'tree\'].UpdateNode';
      $params['varkey'] = 'saved';
      $params['result'] = json_encode($return_article);
      $params['text'] = $ARTICLE->title;
      $params['menu']['editagain'] = '/admin/article/' . $return_article['id'] . '.html';
      $params['menu']['newarticle'] = '/admin/article/' . $ARTICLE->cat . '/new.html';
      $ErrorHandler->setSuccess($params);
   } else
   {
      $ErrorHandler->setError();
   }
   
   $layout = 'results.html';
} elseif ($action == 3)
{
   # CLONE ARTICLE
   $respond = 0;
   if ($FILTER->isValidInteger($id))
   {
      $article = $ARTICLE->cloneArticle($id);
      if ($article)
      {
         $respond = array();
         $respond['id'] = (int) $article['id'];
         $respond['parent'] = (int) $article['cat'];
         $respond['folder'] = 0;
         $respond['startup'] = 0;
         $respond['active'] = (int) $article['active'];
         $respond['title'] = $article['title'];
         $respond['alias'] = '';
         $respond['position'] = $article['position'];

         $respond = json_encode($respond);
      }
   }

   echo $respond;
   exit();
} else
{
   if ($REQUEST->getRequestLast() == 'new')
   {
      $requestPath = $REQUEST->getRequestPath();
      $ARTICLE->setCategory($requestPath[($REQUEST->getRequestLength() - 1)]);
   }

   $SMARTY->assign_by_ref('DataObject', $ARTICLE);
   $layout = 'article.html';
}